package bCommon
{
	
	import flash.events.EventDispatcher;
	
	public class BaseEventDispatcher extends EventDispatcher
	{
		protected var listeners:Array=new Array();
		
		public function BaseEventDispatcher()
		{
			
		}
		
		public function init():void
		{
			removeEventListeners();
		}
		
		public function sendEvent(p:Object):void
		{
			var newEvnt:BaseEvent;
			if(p is BaseEvent)
			{
				newEvnt=BaseEvent(p);
			}
			else
			{
				newEvnt=new BaseEvent(p.type);
				newEvnt.parameter=p.parameter;
			}	
			
			this.dispatchEvent(newEvnt);
		}
		
		override public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, 
												  priority:int=0, useWeakReference:Boolean=false):void
		{
			var obj:Object={type:type,listener:listener,useCapture:useCapture};
			if(!this.isExistEventListener(obj))
			{
				listeners.push();
			}
			super.addEventListener(type,listener,useCapture,priority,useWeakReference);
		}
		
		
		public function removeEventListeners()
		{
			for(var i in listeners)
			{
				if(this.isExistEventListener(listeners[i].type))
				{
					this.removeEventListener(listeners[i].type,listeners[i].listener,listeners[i].useCapture);
				}
				listeners.splice(i,1);
			}
		}
		
		
		private function isExistEventListener(obj:Object):Boolean
		{
			for(var i=0;i<listeners.length;i++)
			{
				if(listeners[i].type==obj.type&&listeners[i].listener==obj.listener)
				{
					return true;
				}
			}
			return false;
		}
		
		public function dispose():void
		{
			removeEventListeners();
			listeners=null;
		}
		
	}
}















